package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.tool.ToolDTO;
import com.etone.smartAudit.dto.toolCase.ToolCaseDTO;
import com.etone.smartAudit.service.ToolService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.tool.ToolBaseVO;
import com.etone.smartAudit.vo.tool.ToolDetailVO;
import com.etone.smartAudit.vo.tool.ToolVO;
import com.etone.smartAudit.vo.toolCase.ToolCaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-07-08 14:49:25
 * @Description ToolController
 */
@RestController
@AllArgsConstructor
@Api(tags = "工具中台", value = "ToolController")
public class ToolController {

    private ToolService toolService;

    @ApiOperation(value = "工具列表《完》")
    @GetMapping("/tools")
    public PagerVO<ToolVO> list(
            @ApiParam(value = "工具名称") @RequestParam(required = false) String name,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return toolService.list(name, page, pageSize);
    }

    @ApiOperation(value = "工具详情《完》")
    @GetMapping("/tools/{id}")
    public ToolDetailVO detail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return toolService.detail(id);
    }

    @ApiOperation(value = "编辑工具详情《完》")
    @PutMapping("/tools/{id}")
    public void edit(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "工具详情表单") @RequestBody @Validated ToolDTO toolDTO

    ) {
        toolService.edit(id, toolDTO);
    }

    @ApiOperation(value = "工具基本信息列表《完》")
    @GetMapping("/tools/simple")
    public List<ToolBaseVO> listSimple() {
        return toolService.listSimple();
    }

    @AdminOperation(module = "工具中台", item = "下载")
    @ApiOperation(value = "下载工具(仅仅用于记录日志)《完》")
    @GetMapping("/tools/{id}/download")
    public void download(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
    }

    @AdminOperation(module = "工具中台", item = "在线使用")
    @ApiOperation(value = "在线使用(仅仅用于记录日志)《完》")
    @GetMapping("/tools/{id}/online")
    public void online(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
    }

    @ApiOperation(value = "工具案例列表《完》")
    @GetMapping("/tools/{id}/cases")
    public PagerVO<ToolCaseVO> caseList(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return toolService.findCaseList(id, page, pageSize);
    }

    @AdminOperation(module = "工具中台", item = "添加案例")
    @ApiOperation(value = "添加工具案例《完》")
    @PostMapping("/tools/{id}/cases")
    public void addCase(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "工具案例表单") @RequestBody @Validated ToolCaseDTO toolCaseDTO
    ) {
        toolService.addCase(id, admin, toolCaseDTO);
    }

    @AdminOperation(module = "工具中台", item = "编辑案例")
    @ApiOperation(value = "编辑工具案例《完》")
    @PutMapping("/tools/{id}/cases/{toolCaseId}")
    public void editCase(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "toolCaseId", required = true) @PathVariable Integer toolCaseId,
            @ApiParam(value = "工具案例表单") @RequestBody @Validated ToolCaseDTO toolCaseDTO
    ) {
        toolService.editCase(id, toolCaseId, admin, toolCaseDTO);
    }

    @AdminOperation(module = "工具中台", item = "下载案例")
    @ApiOperation(value = "案例下载(仅仅用于记录日志和记录下载次数)《完》")
    @GetMapping("/tools/{id}/download/{caseId}")
    public void downloadCase(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "caseId", required = true) @PathVariable Integer caseId
    ) {
        toolService.downloadCase(id, caseId);
    }

    @AdminOperation(module = "工具中台", item = "删除案例")
    @ApiOperation(value = "删除案例《完》(需求变更，预留)")
    @DeleteMapping("/tools/{id}/cases/{caseId}")
    public void deleteCase(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "caseId", required = true) @PathVariable Integer caseId
    ) {
        toolService.deleteCase(id, caseId);
    }

}
